<?php
class UserTableSeeder extends Seeder {

    public function run()
    {
        // to use non Eloquent-functions we need to unguard
        Eloquent::unguard();

        // All existing users are deleted !!!
        DB::table('users')->delete();
		
		// Prepare variable
		$admin_role = DB::table('roles')
							->select('id')
							->where('role_name', 'Administrator')
							->first()
							->id;
		$user_role = DB::table('roles')
							->select('id')
							->where('role_name', 'User')
							->first()
							->id;
							
		$ER_department = DB::table('departments')
							->select('id')
							->where('short_department_name', 'ER')
							->first()
							->id;
		$OR_department = DB::table('departments')
							->select('id')
							->where('short_department_name', 'OR')
							->first()
							->id;
		$LR_department = DB::table('departments')
							->select('id')
							->where('short_department_name', 'LR')
							->first()
							->id;
		
		
        // add user using Eloquent
        $user = new User;
        $user->username = 'admin';
        $user->password = Hash::make('password');
        $user->email = 'admin@localhost';
		$user->first_name = 'Admin';
		$user->last_name = 'Admin';
		$user->status = 'Active';
		$user->role_id = $admin_role;
		$user->department_id = $ER_department;
        $user->save();

        $user = new User;
        $user->username = 'user1';
        $user->password = Hash::make('password');
        $user->email = 'user1@localhost';
		$user->first_name = 'User1';
		$user->last_name = 'User1';
		$user->status = 'Active';
		$user->role_id = $user_role;
		$user->department_id = $ER_department;
        $user->save();	
		
        $user = new User;
        $user->username = 'user2';
        $user->password = Hash::make('password');
        $user->email = 'user2@localhost';
		$user->first_name = 'User2';
		$user->last_name = 'User2';
		$user->status = 'Active';
		$user->role_id = $user_role;
		$user->department_id = $OR_department;
        $user->save();	
		
        $user = new User;
        $user->username = 'user3';
        $user->password = Hash::make('password');
        $user->email = 'user3@localhost';
		$user->first_name = 'User3';
		$user->last_name = 'User3';
		$user->status = 'Active';
		$user->role_id = $user_role;
		$user->department_id = $LR_department;
        $user->save();	
		
		for ($x = 4; $x <= 50; $x++)
		{			
			$user = new User;
			$user->username = 'user'.$x;
			$user->password = Hash::make('password');
			$user->email = 'user'.$x.'@localhost.com';
			$user->first_name = 'User'.$x;
			$user->last_name = 'User'.$x;
			$user->status = 'Active';
			$user->role_id = $user_role;
			$user->department_id = $LR_department;
			$user->save();	
		}

		// $this->command->info('User - First Name >> ' . $user->first_name);
		// $this->command->info('User - Last Name >> ' . $user->last_name);
		// $this->command->info('User - Role Name >> ' . $user->role->role_name);
		// $this->command->info('User - Department Name >> ' . $user->department->department_name);
		// $this->command->info('User - Permission List >> ');
		// foreach($user->department->permissions as $permissionItem)
			// $this->command->info('User - Permission >> ' . $permissionItem->permission_key);
    }
}